package com.shop.vuedemo.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * projectName: vueDemo
 * create on 2021/11/21 22:13
 *
 * @author yanbl
 * @Description : 在自定义filter种使用IOC容器种的bean
 */
@Component
public class ApplicationContextHelper implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        System.out.println(applicationContext);
        applicationContext = context;
    }

    public static  <T> T getBean(Class<T> clazz) {
        if (applicationContext == null) {
            return null;
        }
        return applicationContext.getBean(clazz);
    }
}
